package com.aniu.cameramanager.util;

import android.util.Log;

import java.util.Calendar;
import java.util.Locale;

public class WorkTime {
    private static final String TAG = WorkTime.class.getSimpleName();

    public static final String AM_START_WORK_KEY = "AM_START_WORK";
    public static final String AM_STOP_WORK_KEY = "AM_STOP_WORK";
    public static final String PM_START_WORK_KEY = "PM_START_WORK";
    public static final String PM_STOP_WORK_KEY = "PM_STOP_WORK";

    public static final int AM_START_WORK_DEFAULT_TIME = 540;
    public static final int AM_STOP_WORK_DEFAULT_TIME = 720;
    public static final int PM_START_WORK_DEFAULT_TIME = 840;
    public static final int PM_STOP_WORK_DEFAULT_TIME = 1050;

//    // 工作日开始、结束工作时间
//    public static int amStartWorkHour = 7;
//    public static int amStartWorkMinute = 2;
//    public static int amStopWorkHour = 12;
//    public static int amStopWorkMinute = amStartWorkMinute + 2;
//    public static int pmStartWorkHour = 13;
//    public static int pmStartWorkMinute = 25;
//    public static int pmStopWorkHour = 17;
//    public static int pmStopWorkMinute = amStartWorkMinute + 6;

    // 工作日时间段
    public static final int NON_WORK_DAY = 0; // 非工作日
    public static final int AM_REST = 1; // 上午/凌晨休息时间 (00:00 - amStartWorkHour:amStartWorkMinute)
    public static final int AM_WORK = 2; // 上午工作时间 (amStartWorkHour:amStartWorkMinute - amStopWorkHour:amStopWorkMinute)
    public static final int NOON_REST = 3; // 中午休息时间 (amStopWorkHour:amStopWorkMinute - pmStartWorkHour:pmStartWorkMinute)
    public static final int PM_WORK = 4; // 下午工作时间 (pmStartWorkHour:pmStartWorkMinute - pmStopWorkHour:pmStopWorkMinute)
    public static final int PM_REST = 5; // 下午/晚上休息时间 (pmStopWorkHour:pmStopWorkMinute - 23:59:59)

    public static boolean isNowWorkTime(int amStartWorkTime, int amStopWorkTime, int pmStartWorkTime, int pmStopWorkTime) {
        boolean result = false;

        int workDaySection = getWorkdaySection(Calendar.getInstance(), amStartWorkTime, amStopWorkTime, pmStartWorkTime, pmStopWorkTime);

        if (workDaySection == AM_WORK || workDaySection == PM_WORK) {
            result = true;
        }

        return result;
    }

    /**
     * 获取所处的工作日时间段
     *
     * @param calendar
     * @return
     */
    public static int getWorkdaySection(Calendar calendar, int amStartWorkTime, int amStopWorkTime, int pmStartWorkTime, int pmStopWorkTime) {
        int result = 0;

        if (CalendarUtil.getInstance().isWorkDay(calendar)) {
            Calendar amStartWork = createCalendar(amStartWorkTime);
            Calendar amStopWork = createCalendar(amStopWorkTime);
            Calendar pmStartWork = createCalendar(pmStartWorkTime);
            Calendar pmStopWork = createCalendar(pmStopWorkTime);

            if (calendar.before(amStartWork)) {
                result = AM_REST;

            } else if (calendar.after(amStartWork) && calendar.before(amStopWork)) {
                result = AM_WORK;

            } else if (calendar.after(amStopWork) && calendar.before(pmStartWork)) {
                result = NOON_REST;

            } else if (calendar.after(pmStartWork) && calendar.before(pmStopWork)) {
                result = PM_WORK;

            } else {
                result = PM_REST;
            }

        } else {
            result = NON_WORK_DAY;

        }

        Log.i(TAG, "获取到的时间段为: " + result);
        return result;
    }

    private static Calendar createCalendar(int timeInMinute) {
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, timeInMinute / 60);
        calendar.set(Calendar.MINUTE, timeInMinute % 60);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar;
    }

    public static String timeInt2String(int timeInMinute) {
        return String.format(Locale.getDefault(), "%02d:%02d", timeInMinute/60, timeInMinute%60);
    }
}
